﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PM;
using SMALLERP.SY;
//using SMALLERP.PO;
//////更多大型项目源码http://www.code520.net/
namespace SMALLERP
{
    public partial class Login : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr;

        public Login()
        {
            InitializeComponent();
        }

        //登录用户文本框敲回车键
        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtPwd.Focus();
            }
        }

        //登录密码文本框敲回车键
        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                picLogin_Click(sender, e);
            }
        }

        //登录
        private void picLogin_Click(object sender, EventArgs e)
        {
            errInfo.Clear();

            if (String.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                try
                {
                    errInfo.SetError(txtCode, "用户编码不能为空！");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }

            if (String.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                //try
                //{
                //    errInfo.SetError(txtPwd, "用户密码不能为空！");
                //    return;
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "软件提示");
                //    throw ex;
                //}
                //finally
                //{
                //}
            }

            string strSql = "SELECT LOGONNAME,USER_NAME,UDF024,UDF025,UDF026,USER_ID FROM [USER] WHERE LOGONNAME='" + txtCode.Text + "' AND UDF026='" + txtPwd.Text + "'";
            try
            {
                sdr = db.GetDataReader(strSql);
                sdr.Read();
                if (sdr.HasRows)
                {
                    AppMain POForm = new AppMain();
                   //AppPO POForm = new AppPO();
                    Hide();
                    PropertyClass.ID = sdr["USER_ID"].ToString();
                    PropertyClass.OperatorCode = sdr["LOGONNAME"].ToString();
                    PropertyClass.OperatorName = sdr["USER_NAME"].ToString();
                    //PropertyClass.PassWord = sdr["MA003"].ToString();
                    //PropertyClass.IsAdmin = sdr["MA005"].ToString();
                    //PropertyClass.WeiXinAccount = sdr["MA006"].ToString();
                    PropertyClass.jiaose = sdr["UDF024"].ToString();
                    PropertyClass.gzzx = sdr["UDF025"].ToString();
                    POForm.Show();
                }
                else
                {
                    MessageBox.Show("用户编码或用户密码不正确！", "软件提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("用户编码或用户密码不正确！", "软件提示");
                return;
            }
            finally
            {
                sdr.Close();
            }
        }

        //重置
        private void picReset_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtPwd.Text = "";
        }

        //退出
        private void picQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            PropertyClass.OperatorCode = txtCode.Text;
            FormPassWord formPassWord = new FormPassWord();
            //formPassWord.MdiParent = form;
            formPassWord.StartPosition = FormStartPosition.CenterScreen;
            formPassWord.Tag = "修改密码";
            formPassWord.Show();
        }
    }
          
}